home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / IN231VFD.TAR / internet / IN231VFD / DynaLabel.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  1.1 KB  |  52 lines

  1. //    DynaLabel.java - Label with dynamically changing color
  2. //
  3. //    Copyright (C) 1996 by Dale Gass
  4. //    Non-exclusive license granted to MKS, Inc.
  5. //
  6.  
  7. import java.lang.*;
  8. import java.util.*;
  9. import java.awt.*;
  10. import java.net.*;
  11. import java.applet.*;
  12.  
  13. // DynaLabel - a label with dynamically changing colour
  14.  
  15. public class DynaLabel extends Canvas implements Runnable {
  16.     Thread me;
  17.     Font font;
  18.     String s;
  19.     Random r;
  20.  
  21.     // Constructor
  22.  
  23.     public DynaLabel(String text) {
  24.         s = text;
  25.     font = new Font("TimesRoman", Font.BOLD | Font.ITALIC, 20);
  26.     r = new Random();
  27.     resize(120, 30);
  28.  
  29.     (me = new Thread(this)).start();
  30.     }
  31.  
  32.     // Draw the object with a random colour
  33.  
  34.     public void paint(Graphics g) {
  35.     g.setFont(font);
  36.     g.setColor(new Color(r.nextInt() % 256,
  37.                  r.nextInt() % 256,
  38.                  r.nextInt() % 256));
  39.         g.drawString(s, 5, getFontMetrics(font).getHeight());
  40.     }
  41.  
  42.     // Dynamically update the label
  43.  
  44.     public void run() {
  45.     while (true) {
  46.         try { me.sleep(300); } catch (Exception e) { }
  47.             repaint();
  48.         }
  49.     }
  50. }
  51.  
  52.